package com.gms.service.placemanager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gms.entity.place.PlaceApply;
import com.gms.mapper.placemanager.PlaceApplyMapper;
import com.gms.service.placemanager.PlaceApplyService;
import com.gms.util.ObtainDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PlaceApplyServiceImpl extends ServiceImpl<PlaceApplyMapper, PlaceApply> implements PlaceApplyService {

    @Autowired
    private PlaceApplyMapper placeApplyMapper;

    @Override
    public void InsertPlaceApply(PlaceApply placeApply) {
        placeApplyMapper.insert(placeApply);
    }

    @Override
    public void DeletePlaceApply(Integer applyid,String state) {
        UpdateWrapper<PlaceApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("apply_id",applyid).set("apply_state", state);
        placeApplyMapper.update(null, updateWrapper);
    }

    @Override
    public void UpdateApply(String oldeventid, String neweventid) {
        UpdateWrapper<PlaceApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("eventid",oldeventid).set("eventid", neweventid);
        placeApplyMapper.update(null,updateWrapper);
    }

    @Override
    public void UpdatePlaceApplyState(Integer applyid, String eventid, String type, String state) {
        UpdateWrapper<PlaceApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("apply_state",state);//预约状态
        Map<String,Object> map = new HashMap<>();
        map.put("apply_id",applyid);//预约id
        map.put("apply_type",type);//场地预约类型
        map.put("eventid",eventid);//赛事id
        updateWrapper.allEq(map,false);
        placeApplyMapper.update(null,updateWrapper);
    }

    @Override
    public List<PlaceApply> QueryPlaceApplyByPage(String placeId,Date time) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("apply_state","待审核","待使用");
        queryWrapper.eq("place_no",placeId);
        queryWrapper.eq("apply_date",time);
        List<PlaceApply> applyList = placeApplyMapper.selectList(queryWrapper);
        return applyList;
    }

    @Override
    public List<PlaceApply> QueryDismissPlaceApply(String state) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("apply_state",state);
        return placeApplyMapper.selectList(queryWrapper);
    }

    @Override
    public List<PlaceApply> QueryPlaceApplyByUser(String number, String type, String state,String placetype,String storey) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("number",number);
        map.put("apply_type",type);
        map.put("apply_state",state);
        map.put("place_type",placetype);
        map.put("place_storey",storey);
        queryWrapper.allEq(map,false);
        return placeApplyMapper.selectList(queryWrapper);
    }

    @Override
    public List<PlaceApply> QueryWeekApplyById(Date timeStart, Date timeEnd, String storey, String type,String state) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("apply_date",timeStart,timeEnd);
        Map<String,Object> map = new HashMap<>();
        map.put("place_storey",storey);
        map.put("place_type",type);
        map.put("",state);
        queryWrapper.allEq(map,false);
        return placeApplyMapper.selectList(queryWrapper);
    }

    @Override
    public void InsertUseTime(Integer applyid, String timeStart, String timeEnd) {
        UpdateWrapper<PlaceApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("apply_id",applyid);
        updateWrapper.set("apply_start", timeStart);
        updateWrapper.set("apply_end",timeEnd);
        placeApplyMapper.update(null, updateWrapper);
    }

    @Override
    public List<PlaceApply> SelectPlaceApplyByNow(String number,Date date) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        map.put("number",number);
        map.put("apply_date",date);
        map.put("apply_state","待使用");
        map.put("apply_type","正常");
        queryWrapper.allEq(map);
        return placeApplyMapper.selectList(queryWrapper);
    }

    @Override
    public List<PlaceApply> SelectPlaceApplyByUser(String number,Date date) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("apply_state","待使用","待审核");
        queryWrapper.eq("number",number);
        queryWrapper.gt("apply_date",date);
        return placeApplyMapper.selectList(queryWrapper);
    }

    @Override
    public void UpdatePlaceApply(Integer applyid, String starttime) {
        UpdateWrapper<PlaceApply> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("apply_id",applyid);
        updateWrapper.set("apply_start",starttime);
        placeApplyMapper.update(null,updateWrapper);
    }

    @Override
    public void UpdateAllPlaceApply() {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        Map<String,Object> map = new HashMap<>();
        //组装条件
        map.put("apply_state","待使用");
        queryWrapper.allEq(map,false).lt("apply_date",ObtainDate.nowDate());
        PlaceApply placeApply = new PlaceApply();
        placeApply.setApply_state("已失约");
        placeApplyMapper.update(placeApply,queryWrapper);

        QueryWrapper<PlaceApply> queryWrappered = new QueryWrapper<>();
        Date date = new Date();
        int hours = date.getHours();
        queryWrappered.allEq(map,false).eq("apply_date",ObtainDate.nowDate());
        List<PlaceApply> list = placeApplyMapper.selectList(queryWrappered);
        for (int i=0;i<list.size();i++){
            if(Integer.parseInt(list.get(i).getApply_period().substring(6,8))<=hours){
                PlaceApply placeApplyed = new PlaceApply();
                placeApplyed.setApply_state("已失约");
                placeApplyed.setApply_id(list.get(i).getApply_id());
                placeApplyMapper.updateById(placeApplyed);
            }
        }
    }

    @Override
    public List<PlaceApply> SelectEventPlace(String eventid) {
        QueryWrapper<PlaceApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("eventid",eventid);
        return placeApplyMapper.selectList(queryWrapper);
    }
}
